/*
 * @Description  :
 * @Version      :
 * @Company      :
 * @Author       :
 * @Date         :
 * @LastEditTime : 2023-02-04 17:02:13
 */
#ifndef __TFT_ST7735_H__
#define __TFT_ST7735_H__

#include <xCode.h>

#include <Arduino.h>
#include <TFT_Font_ASCII.h>
#include <TFT_Font_GB2312.h>

/*
 * 此处未考虑代码的可移植性,直接默认SPI2
 * PB13     ------> SPI2_SCK
 * PB15     ------> SPI2_MOSI
 */
#define TFT_WIDTH 	160
#define TFT_HEIGHT 	80

#define TFT_BL 		PC6 // Backlight

#define TFT_CS 		PB12
#define TFT_DC 		PC7
#define TFT_SCK 	PB13
#define TFT_MISO 	PB14
#define TFT_MOSI 	PB15

class TFT_ST7735
{
private:
	int32_t pwrPin;
	int32_t dcPin;
	int32_t csPin;

	uint16_t width;
	uint16_t height;

	uint8_t rotation;

	void writeByte(uint8_t data);
	void writeWord(uint16_t data);
	void writeCommand(uint8_t cmd);

	void drawHanZi1212(uint16_t x, uint16_t y, const char *hanZi,
					   uint16_t color, uint16_t bgColor);
	void drawHanZi1616(uint16_t x, uint16_t y, const char *hanZi,
					   uint16_t color, uint16_t bgColor);
	void drawHanZi2424(uint16_t x, uint16_t y, const char *hanZi,
					   uint16_t color, uint16_t bgColor);
	void drawHanZi3232(uint16_t x, uint16_t y, const char *hanZi,
					   uint16_t color, uint16_t bgColor);

public:
	TFT_ST7735(int32_t pwrPin, int32_t dcPin, int32_t csPin, uint16_t width, uint16_t height, uint8_t rotation = ROTATION_UP);
	~TFT_ST7735();

	enum
	{
		ROTATION_DOWN = 1,
		ROTATION_LEFT,
		ROTATION_UP,
		ROTATION_RIGHT,
	};

	enum TFTColor //画笔颜色 RGB:565
	{
		COLOR_WHITE      = 0xFFFF,
		COLOR_BLACK      = 0x0000,
		COLOR_BLUE       = 0x001F,
		COLOR_BRED       = 0XF81F,   //紫色
		COLOR_GRED       = 0XFFE0,
		COLOR_GBLUE      = 0X07FF,
		COLOR_RED        = 0xF800,
		COLOR_MAGENTA    = 0xF81F,
		COLOR_GREEN      = 0x07E0,
		COLOR_CYAN       = 0x7FFF,
		COLOR_YELLOW     = 0xFFE0,
		COLOR_BROWN      = 0XBC40,   //棕色
		COLOR_BRRED      = 0XFC07,   //棕红色
		COLOR_GRAY       = 0X8430,   //灰色
		COLOR_DARKBLUE   = 0X01CF,   //深蓝色
		COLOR_LIGHTBLUE  = 0X7D7C,   //浅蓝色
		COLOR_GRAYBLUE   = 0X5458,   //灰蓝色
		COLOR_LIGHTGREEN = 0X841F,   //浅绿色
		COLOR_LGRAY      = 0XC618,   //浅灰色
		COLOR_LGRAYBLUE  = 0XA651,   //浅灰蓝色
		COLOR_LBBLUE     = 0X2B12,   //浅棕蓝色
		COLOR_DEEPBLUE   = 0X697D,   //深蓝色 （电流）
		COLOR_DEEPGREEN  = 0X1529,   //深绿色 （温度）
		COLOR_FRESHGREEN = 0X7E6C,   //鲜绿   （流速蒸发）
	};

	SPI_HandleTypeDef handleSPI2;
	DMA_HandleTypeDef handleSPI2DMATx;

	void backlightOn(void);
	void backlightOff(void);

	void init(void);
	void enterSleepMode(void);

public:
	/**
	 * @brief 核心绘图API
	 */
	void startWrite(void);
	bool setAddrWindow(uint16_t xs, uint16_t ys, uint16_t width, uint16_t height);
	void writePixels(uint16_t *colors, uint32_t size);
	void writePixel(uint16_t x, uint16_t y, uint16_t color);
	void endWrite(void);

	/**
	 * @brief 基础绘图API
	 */
	void drawLine(uint16_t xs, uint16_t ys, uint16_t xe, uint16_t ye, uint16_t color);
	void drawRect(uint16_t xs, uint16_t ys, uint16_t xe, uint16_t ye, uint16_t color);
	void drawCircle(uint16_t x, uint16_t y, uint16_t r, uint16_t color);
	void drawBitmap(uint16_t x, uint16_t y, uint16_t width, uint16_t height, const uint8_t bitmap[]);

	void fillRect(uint16_t xs, uint16_t ys, uint16_t xe, uint16_t ye, uint16_t color);
	void fillCircle(uint16_t x, uint16_t y, uint16_t r, uint16_t color);
	void fillScreen(uint16_t color);

	void drawChar(uint16_t x, uint16_t y, const char ch,
				  uint16_t color, uint16_t bgColor, uint8_t size);
	void drawString(uint16_t x, uint16_t y, const char *str,
					uint16_t color, uint16_t bgColor, uint8_t size);
	void drawHanZi(uint16_t x, uint16_t y, const char *hanZi,
				   uint16_t color, uint16_t bgColor, uint8_t size);
};

#ifdef SIMPLE
extern TFT_ST7735 tft;
#endif

#endif
